package mushystuff.dtb.web.controller;

import javax.servlet.http.HttpSession;

import mushystuff.dtb.core.model.UserStory;
import mushystuff.dtb.core.model.UserStory.Estimate;
import mushystuff.dtb.core.model.UserStory.Priority;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping(value="/productBacklogModify.do")
public class ProductBacklogModify extends BaseController {
    
    @ModelAttribute("newUserStory")
    public UserStory newUserStory() {
        return new UserStory();
    }
    
    @ModelAttribute("priorities")
    public Priority[] priorities() {
        return Priority.values();
    }
    
    @ModelAttribute("estimates")
    public Estimate[] estimates() {
        return Estimate.values();
    }

    @RequestMapping(method = RequestMethod.GET)
    public String getView(ModelMap model, HttpSession session) {
        model.addAttribute("backlog", getCurrentProduct(session).getBacklog());
        return "dtb.productBacklog.modify";
    }
    
    @RequestMapping(method = RequestMethod.POST)
    public String addUserStory(@ModelAttribute("newUserStory") UserStory newUserStory, ModelMap model, HttpSession session) {
        productService.addUserStoryToBacklog(getCurrentProduct(session), newUserStory);
        return getView(model, session);
    }
}
